package com.example.web.sys_menu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.utils.ResultUtils;
import com.example.utils.ResultVo;
import com.example.web.sys_menu.entity.MakeMenuTree;
import com.example.web.sys_menu.entity.Menu;
import com.example.web.sys_menu.entity.RouterVo;
import com.example.web.sys_menu.service.IMenuService;
import com.example.web.sys_user.entity.User;
import com.example.web.sys_user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LS
 * @since 2024-02-20
 */
@Api(tags = "菜单接口")
@RestController
@RequestMapping("/api/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;
    @Resource
    private IUserService userService;

    @ApiOperation("新增")
    @PostMapping
    public ResultVo add(@RequestBody Menu menu) {
        menu.setCreateTime(new Date());
        if (menuService.save(menu)) {
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

    @ApiOperation("编辑")
    @PutMapping
    public ResultVo edit(@RequestBody Menu menu) {
        menu.setUpdateTime(new Date());
        if (menuService.updateById(menu)) {
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

    @ApiOperation("删除")
    @DeleteMapping("/{menuId}")
    public ResultVo delete(@PathVariable("menuId") Long menuId) {
        if (menuService.removeById(menuId)) {
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }

    @ApiOperation("列表")
    @GetMapping("/list")
    public ResultVo getList(){
        //排序
        QueryWrapper<Menu> query = new QueryWrapper<>();
        query.lambda().orderByAsc(Menu::getOrderNum);
        //查询出所有的菜单
        List<Menu> list = menuService.list(query);
        //组装树数据
        List<Menu> menuList = MakeMenuTree.makeTree(list, 0L);
        return ResultUtils.success("查询成功",menuList);
    }

    @ApiOperation("上级菜单")
    @GetMapping("/getParent")
    public ResultVo getParent(){
        List<Menu> list = menuService.getParent();
        return ResultUtils.success("查询成功",list);
    }

    @ApiOperation("查询用户菜单")
    @GetMapping("/getMenuList")
    public ResultVo getMenuList(Long userId) {
        //获取用户信息
        User user = userService.getById(userId);
        //获取用户的权限信息
        List<Menu> menuList;
        //判断是否为超级管理员
        if (StringUtils.isNotEmpty(user.getIsAdmin()) && user.getIsAdmin().equals("1")) {
            QueryWrapper<Menu> query=new QueryWrapper<>();
            query.lambda().orderByAsc(Menu::getOrderNum);
            menuList = menuService.list(query);
        } else {
            menuList = menuService.getMenuByUserId(userId);
        }
        //菜单数据，查询类型为 0和1的数据
        List<Menu> collect = Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && StringUtils.isNotEmpty(item.getType()) && !item.getType().equals("2")).collect(Collectors.toList());
        //组装菜单数据类型
        List<RouterVo> router = MakeMenuTree.makeRouter(collect, 0L);
        return ResultUtils.success("查询成功", router);
    }
}

